#include "player1.h"
#include "qgraphicsscene.h"
#include <QGraphicsPixmapItem>
#include <QObject>
#include<QKeyEvent>
#include<Qpixmap>
#include<QTimer>

Player1::Player1(QObject *parent1,QGraphicsItem *parent):QObject(parent1),QGraphicsPixmapItem(parent),
    m_MoveLeftFlag(false), m_MoveRightFlag(false), m_MoveUpFlag(false),m_MoveSpeed(3),
    m_JumpSpeed(8),m_SwingFlag(false),m_StartFlag(false),m_MoveRollFlag(false),m_RacketAngle(-50)
    ,m_RacketAngleSpeed(10),Roll1Flag(false),Roll2Flag(false)
{
    //加载玩家照片
    m_NormalPixmap = QPixmap(":/lng/picture/Player1.png");
    this->setPixmap(m_NormalPixmap);

    // 设置可接收焦点和按键事件
    setFlag(QGraphicsItem::ItemIsFocusable,true);
    setFocus();

    // 创建移动和挥拍的定时器
    m_MoveTimer = new QTimer(this);
    QInputDevice::connect(m_MoveTimer, &QTimer::timeout, this, &Player1::move);
    m_JumpTimer = new QTimer(this);
    QInputDevice::connect(m_JumpTimer, &QTimer::timeout, this, &Player1::move);
    m_RollTimer = new QTimer(this);
    QInputDevice::connect(m_RollTimer, &QTimer::timeout, this, &Player1::move);

    //创建球拍对象
    m_Racket = new Racket(this);
    // 假设球拍把手固定在火柴人图片底部中心位置 少了这步一开始球拍不在火柴人身上
    QPointF FixedPos = pos() + QPointF(boundingRect().width() / 2 - 3, boundingRect().height()/2 - 20);
    m_Racket->SetStickPosition(FixedPos);
    m_Racket->RotateRacket(m_RacketAngle);

}

//析构函数
Player1::~Player1()
{
    delete m_MoveTimer;
    delete m_Racket;
}

//按键检测
void Player1::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
        case Qt::Key_Left:
            m_MoveLeftFlag = true;
            m_MoveTimer->start(10);
            break;
        case Qt::Key_Right:
            m_MoveRightFlag = true;
            m_MoveTimer->start(10);
            break;
        case Qt::Key_Up:
            if(!m_MoveUpFlag)
            {
                m_MoveUpFlag = true;
                m_OriginalY = y();
                m_JumpTimer->start(10);
            }
            break;
        case Qt::Key_Space:
            // 旋转球拍
            if(!m_MoveRollFlag)
            {
                m_MoveRollFlag = true;
                m_RollTimer->start(10);
                m_Racket->RotateRacket(m_RacketAngle);
            }
            break;
        default:
            QGraphicsPixmapItem::keyPressEvent(event);
    }
}
void Player1::keyReleaseEvent(QKeyEvent *event)
{
    switch(event->key())
    {
        case Qt::Key_Left:
            m_MoveLeftFlag = false;
            break;
        case Qt::Key_Right:
            m_MoveRightFlag = false;
            break;
        case Qt::Key_Up://不做操作 在move函数中终止计时器
            break;
        case Qt::Key_Space:
            break;
        default:
            QGraphicsPixmapItem::keyReleaseEvent(event);
    }
    if (!m_MoveLeftFlag && !m_MoveRightFlag)
    {
            m_MoveTimer->stop();
    }
}
void Player1::move()
{
    qreal newX = x();
    qreal newY = y();
    qreal oX = x();
    if (m_MoveLeftFlag)
    {
        newX -= m_MoveSpeed;
    }
    if (m_MoveRightFlag)
    {
        newX += m_MoveSpeed;
    }
    if (m_MoveUpFlag)
    {
        if(m_JumpSpeed>0)//上升阶段
        //由于加速度 速度逐渐减小 直至为0 开始下落
        {
            newY -= m_JumpSpeed;
            m_JumpSpeed -= 0.5;
        }
        else if(y()< m_OriginalY)//下落过程
        {
           newY += abs(m_JumpSpeed);
            m_JumpSpeed -= 0.2;
        }
        else//回到原点
        {
           newY = m_OriginalY;
            m_MoveUpFlag = false;
            m_JumpSpeed = 8; // 重置跳跃速度
            m_JumpTimer->stop();
        }
    }

    // 临时设置位置进行碰撞检测
    setPos(newX, newY);
    QList<QGraphicsItem *> collidingItems = this->collidingItems();
    bool Collided = false;
    for (QGraphicsItem *item : std::as_const(collidingItems)) {
        if (item->type() == QGraphicsRectItem::Type) { // 假设空气墙是 QGraphicsRectItem
            Collided = true;
           // qDebug() << "hello";
            break;
        }
    }
    if (Collided)
    {
        // 发生碰撞，恢复原来的位置
        setPos(oX, newY);  //空气墙不影响上下移动 如果是oY则空气墙跳跃时会卡顿
    }
    else
    {   // 没有碰撞，更新位置
        setPos(newX, newY);
    }

    //球拍旋转
    if (m_MoveRollFlag)
    {
        if(!Roll2Flag&&!Roll1Flag&&m_RacketAngle < 50)
        {
            Roll1Flag = true;
        }
        else if(!Roll2Flag&&m_RacketAngle >= 50)
        {
            Roll2Flag = true;
            Roll1Flag = false;
        }
        else if(m_RacketAngle < -50)
        {
            Roll2Flag = false;
            Roll1Flag = false;
            m_RacketAngle = -50;
            m_RollTimer->stop();
            m_MoveRollFlag = false;
        }
        else if(Roll1Flag)
        {
            m_RacketAngle += 5; // 每次旋转 10 度
            m_Racket->RotateRacket(m_RacketAngle);
        }
        else if(Roll2Flag)
        {
            m_RacketAngle -= 5; // 每次旋转 10 度
            m_Racket->RotateRacket(m_RacketAngle);
        }

    }
    qDebug()<< x()<<"h"<<y();
    //更新球拍位置
    QPointF FixedPos = pos() + QPointF(boundingRect().width() / 2 - 3, boundingRect().height()/2 - 20);
    m_Racket->SetStickPosition(FixedPos);
}
